package labox.innovation.config;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import labox.innovation.util.L2Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 
 * @author bright
 * 
 */
public class RateConfig {
    private static final Logger _log = LoggerFactory.getLogger(RateConfig.class.getName());

    private static final String RATES_CONFIG_FILE = "./config/rates.properties";
    // --------------------------------------------------
    // Rate Settings
    // --------------------------------------------------

    public static float RATE_QUESTS_REWARD;

    public static float RATE_DROP_ADENA;

    public static float RATE_CONSUMABLE_COST;

    public static float RATE_EXTR_FISH;

    public static float RATE_DROP_ITEMS;

    public static float RATE_DROP_ITEMS_BY_RAID;

    public static float RATE_DROP_SPOIL;

    public static int RATE_DROP_MANOR;

    public static float RATE_DROP_QUEST;

    public static float RATE_KARMA_EXP_LOST;

    public static float RATE_SIEGE_GUARDS_PRICE;

    public static float RATE_DROP_COMMON_HERBS;

    public static float RATE_DROP_MP_HP_HERBS;

    public static float RATE_DROP_GREATER_HERBS;

    public static float RATE_DROP_SUPERIOR_HERBS;

    public static float RATE_DROP_SPECIAL_HERBS;

    public static int PLAYER_DROP_LIMIT;

    public static int PLAYER_RATE_DROP;

    public static int PLAYER_RATE_DROP_ITEM;

    public static int PLAYER_RATE_DROP_EQUIP;

    public static int PLAYER_RATE_DROP_EQUIP_WEAPON;

    public static float PET_XP_RATE;

    public static int PET_FOOD_RATE;

    public static float SINEATER_XP_RATE;

    public static int KARMA_DROP_LIMIT;

    public static int KARMA_RATE_DROP;

    public static int KARMA_RATE_DROP_ITEM;

    public static int KARMA_RATE_DROP_EQUIP;

    public static int KARMA_RATE_DROP_EQUIP_WEAPON;

    public static double[] PLAYER_XP_PERCENT_LOST;

    public static void load() {
        _log.info("Loading rate Configuration Files...", Config.SERVER_ID);
        InputStream is = null;
        try {
            L2Properties ratesSettings = new L2Properties();
            is = new FileInputStream(new File(RATES_CONFIG_FILE));
            ratesSettings.load(is);

            RATE_QUESTS_REWARD = Float.parseFloat(ratesSettings.getProperty("RateQuestsReward", "1."));
            RATE_DROP_ADENA = Float.parseFloat(ratesSettings.getProperty("RateDropAdena", "1."));
            RATE_CONSUMABLE_COST = Float.parseFloat(ratesSettings.getProperty("RateConsumableCost", "1."));
            RATE_EXTR_FISH = Float.parseFloat(ratesSettings.getProperty("RateExtractFish", "1."));
            RATE_DROP_ITEMS = Float.parseFloat(ratesSettings.getProperty("RateDropItems", "1."));
            RATE_DROP_ITEMS_BY_RAID = Float.parseFloat(ratesSettings.getProperty("RateRaidDropItems", "1."));
            RATE_DROP_SPOIL = Float.parseFloat(ratesSettings.getProperty("RateDropSpoil", "1."));
            RATE_DROP_MANOR = Integer.parseInt(ratesSettings.getProperty("RateDropManor", "1"));
            RATE_DROP_QUEST = Float.parseFloat(ratesSettings.getProperty("RateDropQuest", "1."));

            RATE_KARMA_EXP_LOST = Float.parseFloat(ratesSettings.getProperty("RateKarmaExpLost", "1."));
            RATE_SIEGE_GUARDS_PRICE = Float.parseFloat(ratesSettings.getProperty("RateSiegeGuardsPrice", "1."));
            RATE_DROP_COMMON_HERBS = Float.parseFloat(ratesSettings.getProperty("RateCommonHerbs", "15."));
            RATE_DROP_MP_HP_HERBS = Float.parseFloat(ratesSettings.getProperty("RateHpMpHerbs", "10."));
            RATE_DROP_GREATER_HERBS = Float.parseFloat(ratesSettings.getProperty("RateGreaterHerbs", "4."));
            RATE_DROP_SUPERIOR_HERBS = Float.parseFloat(ratesSettings.getProperty("RateSuperiorHerbs", "0.8")) * 10;
            RATE_DROP_SPECIAL_HERBS = Float.parseFloat(ratesSettings.getProperty("RateSpecialHerbs", "0.2")) * 10;

            PLAYER_DROP_LIMIT = Integer.parseInt(ratesSettings.getProperty("PlayerDropLimit", "3"));
            PLAYER_RATE_DROP = Integer.parseInt(ratesSettings.getProperty("PlayerRateDrop", "5"));
            PLAYER_RATE_DROP_ITEM = Integer.parseInt(ratesSettings.getProperty("PlayerRateDropItem", "70"));
            PLAYER_RATE_DROP_EQUIP = Integer.parseInt(ratesSettings.getProperty("PlayerRateDropEquip", "25"));
            PLAYER_RATE_DROP_EQUIP_WEAPON = Integer.parseInt(ratesSettings.getProperty("PlayerRateDropEquipWeapon", "5"));
            PET_XP_RATE = Float.parseFloat(ratesSettings.getProperty("PetXpRate", "1."));
            PET_FOOD_RATE = Integer.parseInt(ratesSettings.getProperty("PetFoodRate", "1"));
            SINEATER_XP_RATE = Float.parseFloat(ratesSettings.getProperty("SinEaterXpRate", "1."));
            KARMA_DROP_LIMIT = Integer.parseInt(ratesSettings.getProperty("KarmaDropLimit", "10"));
            KARMA_RATE_DROP = Integer.parseInt(ratesSettings.getProperty("KarmaRateDrop", "70"));
            KARMA_RATE_DROP_ITEM = Integer.parseInt(ratesSettings.getProperty("KarmaRateDropItem", "50"));
            KARMA_RATE_DROP_EQUIP = Integer.parseInt(ratesSettings.getProperty("KarmaRateDropEquip", "40"));
            KARMA_RATE_DROP_EQUIP_WEAPON = Integer.parseInt(ratesSettings.getProperty("KarmaRateDropEquipWeapon", "10"));

            // Initializing table
            PLAYER_XP_PERCENT_LOST = new double[Byte.MAX_VALUE + 1];

            // Default value
            for (int i = 0; i <= Byte.MAX_VALUE; i++)
                PLAYER_XP_PERCENT_LOST[i] = 1.;

            // Now loading into table parsed values
            try {
                String[] values = ratesSettings.getProperty("PlayerXPPercentLost", "0,39-7.0;40,75-4.0;76,76-2.5;77,77-2.0;78,78-1.5").split(";");

                for (String s : values) {
                    int min;
                    int max;
                    double val;

                    String[] vals = s.split("-");
                    String[] mM = vals[0].split(",");

                    min = Integer.parseInt(mM[0]);
                    max = Integer.parseInt(mM[1]);
                    val = Double.parseDouble(vals[1]);

                    for (int i = min; i <= max; i++)
                        PLAYER_XP_PERCENT_LOST[i] = val;
                }
            } catch (Exception e) {
                _log.error("Error while loading Player XP percent lost", Config.SERVER_ID, e);
            }
        } catch (Exception e) {
            _log.error("Failed to Load " + RATES_CONFIG_FILE + " File.", e);
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                _log.error("InputStream close occured IOException");
            }
        }
    }
}
